//
//  FSWToastAlertView.m
//  quhuolang
//
//  Created by 曹欣欣 on 2018/3/10.
//  Copyright © 2018年 fusw. All rights reserved.
//

#import "FSWToastAlertView.h"


@interface FSWToastAlertView()

@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet XCBaseView *alertBackView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *subLabel;

@property (weak, nonatomic) IBOutlet UIButton *okButton;
@property (weak, nonatomic) IBOutlet XCBaseButton *cancelButton;


@property (nonatomic, copy) toastOkBlock okBlock;
@property (nonatomic, copy) toastCancelBlock cancelBlock;

@end

@implementation FSWToastAlertView

-(void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
//    self.alertBackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
}

+(instancetype)initCustomView{
    return [[NSBundle mainBundle] loadNibNamed:@"FSWToastAlertView" owner:nil options:nil].firstObject;
}

- (void)show:(NSInteger)type addMessage:(NSString *)message andOkBlock:(toastOkBlock)okBlock andCancelBlock:(toastCancelBlock)cancelBlock{
    
    [self type:type addMessage:message andOkBlock:okBlock andCancelBlock:cancelBlock];
    
    self.frame = [UIScreen mainScreen].bounds;
    self.windowLevel = UIWindowLevelAlert;
    
    [UIView animateWithDuration:0.5
                          delay:0
         usingSpringWithDamping:1.0
          initialSpringVelocity:1.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         //                         @strongify(self)
                         //                         self.alertView.alpha = 1;
                         //                         self.backButton.alpha = 0.3;
                     }
                     completion:^(BOOL finished) {
                         //                         @strongify(self)
                         //                         self.userInteractionEnabled = YES;
                     }];
}

#pragma mark - Private Method
- (void)type:(NSInteger)type addMessage:(id)message andOkBlock:(toastOkBlock)okBlock andCancelBlock:(toastCancelBlock)cancelBlock{
    self.okBlock = okBlock;
    self.cancelBlock = cancelBlock;
    
    if(![FSWHelperTool isBlankString:self.titleColor]){
        self.titleLabel.textColor = [UIColor colorNamed:self.titleColor];
    }
    if(![FSWHelperTool isBlankString:self.messageColor]){
        self.subLabel.textColor = [UIColor colorNamed:self.messageColor];
    }
    if(![FSWHelperTool isBlankString:self.cancelTextColor]){
        [self.cancelButton setTitleColor:[UIColor colorNamed:self.cancelTextColor] forState:UIControlStateNormal];
    }
    if(![FSWHelperTool isBlankString:self.okTextColor]){
        [self.okButton setTitleColor:[UIColor colorNamed:self.okTextColor] forState:UIControlStateNormal];
    }
     
    if(![FSWHelperTool isBlankString:message]){
        self.subLabel.text = message;
    }else{
        self.subLabel.text = @"";
    }
    
    if(![FSWHelperTool isBlankString:self.okText]){
        [self.okButton setTitle:self.okText forState:UIControlStateNormal];
    }
    
    if(![FSWHelperTool isBlankString:self.cancelText]){
        [self.cancelButton setTitle:self.cancelText forState:UIControlStateNormal];
    }
    
    if(![FSWHelperTool isBlankString:self.title]){
        self.titleLabel.text = self.title;
    }else{
        self.titleLabel.text = @"";
    }
    
    switch (type) {
        case 1:
            self.cancelButton.hidden = YES;
            self.okButton.hidden = NO;
            break;
        case 2:
            self.okButton.hidden = YES;
            self.cancelButton.hidden = NO;
            break;
        default:
            self.okButton.hidden = NO;
            self.cancelButton.hidden = NO;
            break;
    }
}
 
- (IBAction)backButton:(id)sender {
    //      [self dismiss];
}
- (IBAction)okButton:(id)sender {
    [self dismiss];
    if (self.okBlock) {
        self.okBlock();
    }
}
- (IBAction)cancelButton:(id)sender {
    [self dismiss];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

-(void)dismiss{
    @weakify(self);
    [UIView animateWithDuration:0.1
                          delay:0
         usingSpringWithDamping:0.9
          initialSpringVelocity:0.9
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
//                         self.backButton.alpha = 0;
                     }
                     completion:^(BOOL finished) {
                         @strongify(self);
                         self.userInteractionEnabled = NO;
                         [self resignKeyWindow];
                         [self setHidden:YES];
                         [[UIApplication sharedApplication].keyWindow makeKeyWindow];
                     }];
}
@end

